package cn.yuemouren.leetcode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Timi
 * @Description: 290. 单词规律
 * @Date: 2020/8/6 8:35
 */
public class Test_290 {

    public boolean wordPattern(String pattern, String str) {
        Map<Character,String> map = new HashMap<>();
        char[] patterns = pattern.toCharArray();
        String[] s = str.split(" ");
        if(patterns.length != s.length)
            return false;
        for(int i = 0; i < patterns.length; i++) {
            char c = patterns[i];
            String s1 = s[i];
            if (!map.containsKey(c)) {
                if (map.containsValue(s1)) {
                    return false;
                }
                map.put(c, s1);
            } else {
                if (!map.get(c).equals(s1)) {
                    return false;
                }
            }
        }
        return true;
    }
}
